'use strict';
exports.__esModule = true;
exports.ensureNodeData = ensureNodeData;
exports.getNodeType = getNodeType;
exports.appendChildren = appendChildren;
exports.removeNode = removeNode;
exports.getAccessor = getAccessor;
exports.setAccessor = setAccessor;
exports.getNodeAttributes = getNodeAttributes;
exports.getRawNodeAttributes = getRawNodeAttributes;
var _constants = require('../constants');
var _util = require('../util');
var _hooks = require('../hooks');
function ensureNodeData(node) {
return node[_constants.ATTR_KEY] || (node[_constants.ATTR_KEY] = {});
}
function getNodeType(node) {
return node.nodeType;
}
/** Append multiple children to a Node.
* Uses a Document Fragment to batch when appending 2 or more children
* @private
*/
function appendChildren(parent, children) {
var len = children.length,
many = len > 2,
into = many ? document.createDocumentFragment() : parent;
Ifor (var i = 0; i < len; i++) {
into.appendChild(children[i]);
}if (many) parent.appendChild(into);
}
/** Removes a given DOM Node from its parent. */
function removeNode(node) {
var p = node.parentNode;
if (p) p.removeChild(node);
}
/** Retrieve the value of a rendered attribute
* @private
*/
function getAccessor(node, name, value, cache) {
if (name !== 'type' && name !== 'style' && name in node) return node[name];
var attrs = node[_constants.ATTR_KEY];
if (cache !== false && attrs && _util.hasOwnProperty.call(attrs, name)) return attrs[name];
if (name === 'class') return node.className;
if (name === 'style') return node.style.cssText;
return value;
}
/** Set a named attribute on the given Node, with special behavior for some names and event handlers.
* If `value` is `null`, the attribute/handler will be removed.
* @param {Element} node An element to mutate
* @param {string} name The name/key to set, such as an event or attribute name
* @param {any} value An attribute value, such as a function to be used as an event handler
* @param {any} previousValue The last value that was set for this name/node pair
* @private
*/
function setAccessor(node, name, value) {
if (name === 'class') {
node.className = value || '';
} else if (name === 'style') {
node.style.cssText = value || '';
} else if (name === 'dangerouslySetInnerHTML') {
if (value && value.__html) node.innerHTML = value.__html;
} else if (name === 'key' || name in node && name !== 'type') {
node[name] = value;
if (_util.falsey(value)) node.removeAttribute(name);
} else {
setComplexAccessor(node, name, value);
}
ensureNodeData(node)[name] = value;
}
/** For props without explicit behavior, apply to a Node as event handlers or attributes.
* @private
*/
function setComplexAccessor(node, name, value) {
if (name.substring(0, 2) === 'on') {
var _type = normalizeEventName(name),
l = node._listeners || (node._listeners = {}),
fn = !l[_type] ? 'add' : !value ? 'remove' : null;
if (fn) node[fn + 'EventListener'](_type, eventProxy);
l[_type] = value;
return;
}
var type = typeof value;
if (_util.falsey(value)) {
node.removeAttribute(name);
} else if (type !== 'function' && type !== 'object') {
node.setAttribute(name, value);
}
}
/** Proxy an event to hooked event handlers
* @private
*/
function eventProxy(e) {
var fn = this._listeners[normalizeEventName(e.type)];
Eif (fn) return fn.call(this, _hooks.optionsHook('event', e) || e);
}
/** Convert an Event name/type to lowercase and strip any "on*" prefix.
* @function
* @private
*/
var normalizeEventName = _util.memoize(function (t) {
return t.replace(/^on/i, '').toLowerCase();
});
/** Get a hashmap of node properties, preferring preact's cached property values over the DOM's
* @private
*/
function getNodeAttributes(node) {
return node[_constants.ATTR_KEY] || getRawNodeAttributes(node) || _constants.EMPTY;
// let list = getRawNodeAttributes(node),
// l = node[ATTR_KEY];
// return l && list ? extend(list, l) : (l || list || EMPTY);
}
/** Get a node's attributes as a hashmap, regardless of type.
* @private
*/
function getRawNodeAttributes(node) {
var list = node.attributes;
Iif (!list || !list.getNamedItem) return list;
return getAttributesAsObject(list);
}
/** Convert a DOM `.attributes` NamedNodeMap to a hashmap.
* @private
*/
function getAttributesAsObject(list) {
var attrs = undefined;
for (var i = list.length; i--;) {
var item = list[i];
if (!attrs) attrs = {};
attrs[item.name] = item.value;
}
return attrs;
}
|